/*
 * @Author: LSC
 */
const Mock = require('mockjs')
let imgList = require('./imgList')
let CommodityList = []
const count = 11



let commoditys = [
  {
    name: 'Customize for baby',
    img: 0,
    price: 8
  },
  {
    name: 'Customize for pet',
    img: 1,
    price: 10
  },
  {
    name: 'Metalcard black',
    img: 2,
    price: 7
  },
  {
    name: 'Metalcard blue',
    img: 3,
    price: 7
  },
  {
    name: 'Metalcard stripe',
    img: 4,
    price: 7.5
  },
  {
    name: 'Metalcard_world',
    img: 5,
    price: 7
  },
  {
    name: 'Metalcard horizontal',
    img: 6,
    price: 7
  },
  {
    name: 'Wooden card',
    img: 7,
    price: 7
  },
  {
    name: 'Biometric card',
    img: 8,
    price: 7
  },
  {
    name: 'key fobs',
    img: 9,
    price: 10
  },
  {
    name: 'other products',
    img: 10,
    price: 12
  }
]
for (let i = 0; i < count; i++) {
  let type, groupId, cardDirectionH, cardsH, logoH
  if (i < 9) {
    type = {
      value: "type0",
      label: "card",
    }
    cardDirectionH = ['Horizontal']
    cardsH = imgList.cardsH
    logoH = imgList.logoH
  } else {
    type = {
      value: "type1",
      label: "general product",
    }
    cardDirectionH = []
    cardsH = []
    logoH = ''
  }

  if (i < 2) {
    groupId = [0]
  } else if (i < 9) {
    groupId = [1]
  } else {
    groupId = [2]
  }
  CommodityList.push(Mock.mock({
    id: i,
    groupId,
    img: commoditys[i].img,
    name: commoditys[i].name,
    'code|100000-999999': 100000,
    type,
    price: commoditys[i].price,
    'status|1': [{ value: 'status0', label: 'On sale' }],
    'num': i + 1,
    time: new Date(),
    cardDirectionH,
    cardDirectionV: [],
    cardColorH: [],
    cardColorV: [],
    cardsH,
    logoH,
    cardsV: [],
    logoV: '',
    detailsImage: '',
    editPrice: false,
    editName: false,
  }))
}

module.exports = [
  // username search
  {
    url: '/vue-element-admin/get/commodityList',
    type: 'get',
    response: config => {

      let { page = 1, limit = 10, name, type, group, status, groupId } = config.query
      let searchArr = JSON.parse(JSON.stringify(CommodityList))

      //条件搜索
      if (name) {
        name = name.replace(/\+/g, " ");
        searchArr = searchArr.filter(item => item.name === name)
      }
      if (type) {
        searchArr = searchArr.filter(item => item.type.value === type)
      }
      if (status) {
        searchArr = searchArr.filter(item => item.status.value === status)
      }
      if (groupId !== "") {
        let arr = groupId.split(',')
        searchArr = searchArr.filter(item => {
          if (arr.includes(item.id + '')) {
            return item
          }
        }
        )
      }

      const pageList = searchArr.filter((item, index) => index < limit * page && index >= limit * (page - 1))
      return {
        code: 20000,
        count: searchArr.length,
        data: pageList
      }
    }
  },
  {
    url: '/vue-element-admin/getCommodityById',
    type: 'get',
    response: config => {
      const { id } = config.query
      let searchArr = JSON.parse(JSON.stringify(CommodityList))
      searchArr = searchArr.filter(item => item.id == id)[0]
      return {
        code: 20000,
        data: searchArr
      }
    }
  },
  {
    url: '/vue-element-admin/setCommodity',
    type: 'post',
    response: config => {
      let { id, cardColorH, cardColorV, cardDirectionH, cardDirectionV, cardsH, cardsV, checked1, checked2, coverImage, detailsImage, group, groupId, logoH, logoV, productDescription, shipping,
        img, productPrice, productName, type, cardDirection, cardColor, cardsVList, discountPrice, taxRate, status } = config.body
      type = [{ value: 'type0', label: 'card' }, { value: 'type1', label: 'general product' }].filter(item => item.value === type)[0]
      //条件搜索
      CommodityList = CommodityList.map(item => {
        if (item.id == id) {
          item.img = img
          item.price = productPrice
          item.name = productName
          item.type = type
          item.cardDirection = cardDirection
          item.cardColor = cardColor
          item.cardsVList = cardsVList
          item.discountPrice = discountPrice
          item.taxRate = taxRate
          item.status = status

          item.cardColorH = cardColorH
          item.cardColorV = cardColorV
          item.cardDirectionH = cardDirectionH
          item.cardDirectionV = cardDirectionV
          item.cardsH = cardsH
          item.cardsV = cardsV
          item.checked1 = checked1
          item.checked2 = checked2
          item.coverImage = coverImage
          item.detailsImage = detailsImage
          item.group = group
          item.groupId = groupId
          item.logoH = logoH
          item.logoV = logoV
          item.productDescription = productDescription
          item.shipping = shipping
        }
        return item
      })

      return {
        code: 20000,
        data: {
          message: 'success'
        }
      }
    }
  },
  {
    url: '/vue-element-admin/add/commodity',
    type: 'post',
    response: config => {
      let oldArr = JSON.parse(JSON.stringify(CommodityList))
      let { cardColorH, cardColorV, cardDirectionH, cardDirectionV, cardsH, cardsV, checked1, checked2, coverImage, detailsImage, group, groupId, logoH, logoV, productDescription, shipping,
        img, productPrice, productName, type, cardDirection, cardColor, cardsVList, discountPrice, taxRate, status } = config.body
      type = [{ value: 'type0', label: 'card' }, { value: 'type1', label: 'general product' }].filter(item => item.value === type)[0]
      if (status) {
        status = JSON.parse(status)
      } else {
        status = { value: 'status0', label: 'On sale' }
      }

      CommodityList.push(Mock.mock({
        id: oldArr.length,
        img,
        type,
        status,
        price: productPrice,
        num: oldArr.length + 1,
        time: new Date(),
        name: productName,
        cardDirection,
        cardColor,
        cardsVList,
        discountPrice,
        taxRate,
        cardColorH,
        cardColorV,
        cardDirectionH,
        cardDirectionV,
        cardsH,
        cardsV,
        checked1,
        checked2,
        coverImage,
        detailsImage,
        group,
        groupId,
        logoH,
        logoV,
        productDescription,
        shipping,
      }))
      return {
        code: 20000,
        data: {
          message: 'success'
        }
      }
    }
  },
  {
    url: '/vue-element-admin/saveCommodity',
    type: 'get',
    response: config => {
      let { id, status, price, name } = config.query
      //条件搜索
      CommodityList = CommodityList.map(item => {
        if (item.id == id) {
          if (status) {
            item.status = JSON.parse(status)
          }
          if (price) {
            item.price = price
          }
          if (name) {
            item.name = name
          }
        }
        return item
      })

      return {
        code: 20000,
        data: {
          message: 'success'
        }
      }
    }
  },
  // 修改产品分组
  {
    url: '/vue-element-admin/setCommodityGroup',
    type: 'get',
    response: config => {
      let { newCommodity } = config.query
      newCommodity = JSON.parse(newCommodity)
      // console.log('newCommodity===', newCommodity);
      //条件搜索
      CommodityList = CommodityList.map(item => {
        if (item.id == newCommodity.id) {
          item = newCommodity
        }
        return item
      })

      return {
        code: 20000,
        data: {
          message: 'success'
        }
      }
    }
  },
]
